<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        // echo md5('123456');
       $this->display();         
    }
    public function search_info(){
    	
    	$get = I('get.');
    	// dump($get);exit;
    	// if(empty($get['sn'])){
    	// 	die("<script>alert('没有找到此信息！');history.back(-1);</script>");
    	// }else{
    	// 	$map['qr_sn'] = $post['sn']; 
    	// }
    	$map['qr_sn'] = empty($_REQUEST['sn']) ? '' : trim($_REQUEST['sn']);

        $this -> assign('qr_sn',$map['qr_sn']);
        //点工厂网址
        $url = M('config')->where("name = 'fw_url'")->getField('value');
        // $map['qr_sn'] = $post['sn'];   	
    	$data = M('security_qr')->where($map)->find();
        if($data){
            $goods_id = $data['goods_id'];
            //商品轮播图
            $imgs = M('goods_gallery')->order('img_desc desc')->where('goods_id='.$goods_id)->select();
            $imgzhu = M('cat_goods')->where('goods_id='.$goods_id)->getField('goods_img');
            // dump($imgs);
            // dump($imgzhu);exit;
            $this->assign('imgs',$imgs);
            $this->assign('imgzhu',$imgzhu);
            //轮播图片
            // 基本信息图片
            $lubojiben = M('security_lunbo')
                        ->where(array('goods_id'=>$goods_id,'lunbo_weizhi'=>'1','is_open'=>'1'))
                        ->order('sort desc')
                        ->select();
            // 检测信息图片
             $lubojc = M('security_lunbo')
                        ->where(array('goods_id'=>$goods_id,'lunbo_weizhi'=>'2','is_open'=>'1'))
                        ->order('sort desc')
                        ->select();
            // 检测信息图片
             $luboqt = M('security_lunbo')
                        ->where(array('goods_id'=>$goods_id,'lunbo_weizhi'=>'3','is_open'=>'1'))
                        ->order('sort desc')
                        ->select();

            //检测信息弹框

            M('security_qr')->where("qr_sn='".$_REQUEST['sn']."'")->setInc('y_number',1);//修改查询次数
            //店铺名称
            
            $qr_sn=$_REQUEST['sn'];
            $qr_info=M('Security_qr')->field('goods_id,y_number')->where("qr_sn='{$qr_sn}'")->find();
            $goods_info=M('Cat_goods')->where("goods_id={$qr_info['goods_id']}")->find();
            if ($goods_info['supplier_id']) {
                
                $supplier_info=M("Cat_supplier")->field('supplier_name')->where("supplier_id={$goods_info['supplier_id']}")->find();
            }else{
                $supplier_info=[];
            }
            $info=array(
                'goods_name'=>$goods_info['goods_name'],
                'y_number'=>$qr_info['y_number'],
                'qr_sn'=>$qr_sn,

            );
            if(!empty($supplier_info)){
                $info['producer']=$supplier_info['supplier_name'];
            }else{
                $info['producer']='点工厂';
            }

            // 获取商品分类及检测信息
            $cat_id = M('cat_goods')->where(array('supplier_id'=>$data['supplier_id'],'goods_id'=>$goods_id))->getField('cat_id');
            $jb_field = M('field')->where(array('term_id'=>$cat_id,'position'=>'1')) -> field('field,name') ->select();
            $jc_field = M('field')->where(array('term_id'=>$cat_id,'position'=>'2')) -> field('field,name') ->select();
            $qt_field = M('field')->where(array('term_id'=>$cat_id,'position'=>'3')) -> field('field,name') ->select();
            
            if (!empty($jb_field)) {
                foreach ($jb_field as $jb_k => $jb_v) {
                    $jb_field_arr[] = $jb_v['field'];
                }

                $jb_str = implode(',',$jb_field_arr);
                $jb_info = M('security_info_'.$cat_id) -> where(array('goods_id'=>$goods_id)) -> field($jb_str) -> find();
                foreach ($jb_info as $k => $v) {
                    foreach ($jb_field as $k1 => $v1) { 
                        if ($k == $v1['field']) {
                            $jb_info[$k] = array('value'=>$v,'name'=>$v1['name']);
                        }
                    }
                }
            }else{
                $jb_info = [];
            }

            if (!empty($jc_field)) {
                foreach ($jc_field as $jc_k => $jc_v) {
                    $jc_field_arr[] = $jc_v['field'];
                }
                $jc_str = implode(',',$jc_field_arr);

                $jc_info = M('security_info_'.$cat_id) -> where(array('goods_id'=>$goods_id)) -> field($jc_str) -> find();
                foreach ($jc_info as $k => $v) {
                    foreach ($jc_field as $k1 => $v1) { 
                        if ($k == $v1['field']) {
                            $jc_info[$k] = array('value'=>$v,'name'=>$v1['name']);
                        }
                    }
                }
            }else{
                $jc_info = [];
            }

            if (!empty($qt_field)) {
                foreach ($qt_field as $qt_k => $qt_v) {
                    $qt_field_arr[] = $qt_v['field'];
                }  
                $qt_str = implode(',',$qt_field_arr);
                $qt_info = M('security_info_'.$cat_id) -> where(array('goods_id'=>$goods_id)) -> field($qt_str) -> find();
                foreach ($qt_info as $k => $v) {
                    foreach ($qt_field as $k1 => $v1) { 
                        if ($k == $v1['field']) {
                            $qt_info[$k] = array('value'=>$v,'name'=>$v1['name']);
                        }
                    }
                }
            }else{
                $qt_info = [];
            }

            $this->assign('lubojiben',$lubojiben);
            $this->assign('lubojc',$lubojc);
            $this->assign('luboqt',$luboqt);
            $this->assign('url',$url);
            $this->assign('info',$info);
            // 将查询到的信息发送到前台
            $this->assign('jb_info',$jb_info);
            $this->assign('jc_info',$jc_info);
            $this->assign('qt_info',$qt_info);
            $this->display('info');
        }else{
            die("<script>alert('没有找到此信息！');history.back(-1);</script>");
        }
    }
}